#include "mpLog.h"
#include "mpHttpUrlTests.h"

mpHttpUrlTests::mpHttpUrlTests()
{
}

void mpHttpUrlTests::initTestCase(){}
void mpHttpUrlTests::cleanupTestCase(){}

void mpHttpUrlTests::httpurl_parse_exec(const MP::HTTP::Url & url,
                                        const std::string & scheme,
                                        const std::string & host,
                                        unsigned short port,
                                        const std::string & path,
                                        const std::string & tostring){


    QVERIFY2(url.getScheme() == scheme,"httpurl_parse-scheme");
    QVERIFY2(url.getHost() == host,"httpurl_parse-host");
    QVERIFY2(url.getPort() == port,"httpurl_parse-port");
    QVERIFY2(url.getRelativeUri() == path,"httpurl_parse-relativeUri");
    QVERIFY2(url.toString() == tostring,"httpurl_parse-tostring");


    //    std::string tmp = httpUrl1.toString();
    //    tmp.clear();
    //    QVERIFY2(tmp == url,"httpurl_parse-toString");
}





void mpHttpUrlTests::httpurl_parse(const std::string & url,
                                   const std::string & scheme,
                                   const std::string & host,
                                   unsigned short port,
                                   const std::string & path,
                                   const std::string & tostring){

    MP::HTTP::Url httpUrl1(url);

    httpurl_parse_exec(httpUrl1,scheme,host,port,path,tostring);
}


void mpHttpUrlTests::httpurl_parse2(const MP::HTTP::Url & baseurl,
                                    const std::string & relativeUri,
                                    const std::string & scheme,
                                    const std::string & host,
                                    unsigned short port,
                                    const std::string & path,
                                    const std::string & tostring){

    MP::HTTP::Url httpUrl2(baseurl,relativeUri);
    httpurl_parse_exec(httpUrl2,scheme,host,port,path,tostring);
}



void mpHttpUrlTests::case_httpurl_create1(){
#ifdef TEST_STRATEGY_COMPLETED

    //schema://host:port
    httpurl_parse("https://translate.google.cn/","https","translate.google.cn",443,
                  "/",
                  "https://translate.google.cn/");

    httpurl_parse("https://translate.google.cn","https","translate.google.cn",443,
                  "",
                  "https://translate.google.cn");

    httpurl_parse("http://192.168.3.1:8080"
                  ,"http","192.168.3.1",8080,
                  "",
                  "http://192.168.3.1:8080");

    //schema://host:port/path
    httpurl_parse("https://blog.csdn.net/tzhope/article/","https","blog.csdn.net",443,
                  "/tzhope/article/",
                  "https://blog.csdn.net/tzhope/article/");

    httpurl_parse("http://192.168.3.1:8080/xyz"
                  ,"http","192.168.3.1",8080,
                  "/xyz",
                  "http://192.168.3.1:8080/xyz");



    //schema://host:port/path#fragment
    httpurl_parse("https://translate.google.cn/abc/def/#product","https","translate.google.cn",443,
                  "/abc/def/#product",
                  "https://translate.google.cn/abc/def/#product");

    httpurl_parse("https://translate.google.cn/abc/def#product","https","translate.google.cn",443,
                  "/abc/def#product",
                  "https://translate.google.cn/abc/def#product");

    httpurl_parse("http://192.168.3.1:8080/xyz#abc"
                  ,"http","192.168.3.1",8080,
                  "/xyz#abc",
                  "http://192.168.3.1:8080/xyz#abc");

    //schema://host:port/path?queries
    httpurl_parse("https://translate.google.cn/abc/def?sl=auto&tl=en&text=sign&op=translate"
                  ,"https","translate.google.cn",443,
                  "/abc/def?sl=auto&tl=en&text=sign&op=translate",
                  "https://translate.google.cn/abc/def?sl=auto&tl=en&text=sign&op=translate");

    httpurl_parse("http://192.168.3.1:8080/xyz?abc="
                  ,"http","192.168.3.1",8080,
                  "/xyz?abc=",
                  "http://192.168.3.1:8080/xyz?abc=");

    //schema://host:port/path?queries#fragment
    httpurl_parse("https://translate.google.cn/abc/def?sl=auto&tl=en&text=sign&op=translate#product"
                  ,"https","translate.google.cn",443,
                  "/abc/def?sl=auto&tl=en&text=sign&op=translate#product",
                  "https://translate.google.cn/abc/def?sl=auto&tl=en&text=sign&op=translate#product");

    httpurl_parse("http://192.168.3.1:8080/xyz?abc=#product"
                  ,"http","192.168.3.1",8080,
                  "/xyz?abc=#product",
                  "http://192.168.3.1:8080/xyz?abc=#product");


    //schema://host:port?queries
    httpurl_parse("http://translate.google.cn/?sl=auto&tl=en&text=sign&op="
                  ,"http","translate.google.cn",80,
                  "/?sl=auto&tl=en&text=sign&op=",
                  "http://translate.google.cn/?sl=auto&tl=en&text=sign&op=");

    httpurl_parse("http://translate.google.cn?sl=auto&tl=en&text=sign&op="
                  ,"http","translate.google.cn",80,
                  "?sl=auto&tl=en&text=sign&op=",
                  "http://translate.google.cn?sl=auto&tl=en&text=sign&op=");


    httpurl_parse("http://192.168.3.1:8080?abc="
                  ,"http","192.168.3.1",8080,
                  "?abc=",
                  "http://192.168.3.1:8080?abc=");

    //schema://host:port#fragment
    httpurl_parse("http://192.168.3.1:8080/#product"
                  ,"http","192.168.3.1",8080,
                  "/#product",
                  "http://192.168.3.1:8080/#product");

    httpurl_parse("http://192.168.3.1:8080#product"
                  ,"http","192.168.3.1",8080,
                  "#product",
                  "http://192.168.3.1:8080#product");

#endif

}

void mpHttpUrlTests::case_httpurl_create2(){
#ifdef TEST_STRATEGY_COMPLETED

    MP::HTTP::Url httpBaseUrl_01("https://translate.google.cn/");

    httpurl_parse2(httpBaseUrl_01,"/","https","translate.google.cn",443,
                   "/",
                   "https://translate.google.cn/");

    httpurl_parse2(httpBaseUrl_01,"/abc","https","translate.google.cn",443,
                   "/abc",
                   "https://translate.google.cn/abc");

    httpurl_parse2(httpBaseUrl_01,"/abc?key=","https","translate.google.cn",443,
                   "/abc?key=",
                   "https://translate.google.cn/abc?key=");

    httpurl_parse2(httpBaseUrl_01,"/abc?key=#x1","https","translate.google.cn",443,
                   "/abc?key=#x1",
                   "https://translate.google.cn/abc?key=#x1");

    httpurl_parse2(httpBaseUrl_01,"/?key=#x1","https","translate.google.cn",443,
                   "/?key=#x1",
                   "https://translate.google.cn/?key=#x1");

    httpurl_parse2(httpBaseUrl_01,"?key=#x1","https","translate.google.cn",443,
                   "?key=#x1",
                   "https://translate.google.cn?key=#x1");



    httpurl_parse2(httpBaseUrl_01,"#x1","https","translate.google.cn",443,
                   "#x1",
                   "https://translate.google.cn#x1");

    httpurl_parse2(httpBaseUrl_01,"/#x1","https","translate.google.cn",443,
                   "/#x1",
                   "https://translate.google.cn/#x1");

    httpurl_parse2(httpBaseUrl_01,"/?#x1","https","translate.google.cn",443,
                   "/?#x1",
                   "https://translate.google.cn/?#x1");

    httpurl_parse2(httpBaseUrl_01,"?/#x1","https","translate.google.cn",443,
                   "?/#x1",
                   "https://translate.google.cn?/#x1");

    httpurl_parse2(httpBaseUrl_01,"#?/x1","https","translate.google.cn",443,
                   "#?/x1",
                   "https://translate.google.cn#?/x1");


#endif

}

void mpHttpUrlTests::case_httpurl_create3(){

#ifdef TEST_STRATEGY_COMPLETED

    //    explicit Url(const std::string & schema,
    //                 const std::string & host,
    //                 unsigned short port,
    //                 const std::string & relativeUri);

    MP::HTTP::Url url("http","127.0.0.1",8080,"#?/x1");
    httpurl_parse_exec(url,"http","127.0.0.1",8080,"#?/x1","http://127.0.0.1:8080#?/x1");


#endif
}

